package com.doyens.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doyens.reggie.commons.BaseContext;
import com.doyens.reggie.commons.R;
import com.doyens.reggie.domain.ShoppingCart;
import com.doyens.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    // 减少购物车的某一个商品数量

    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if(dishId != null){//菜
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{ //套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one.getNumber() == 1){
            // 删除
            shoppingCartService.removeById(one.getId());
        }else{
           one.setNumber(one.getNumber() - 1);
           shoppingCartService.updateById(one);
        }

        return R.success("成功");

    }

    @DeleteMapping("clean")
    public R<String> cleanCart(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("已清空");
    }

    // 查询自己购物车数据
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> cartList = shoppingCartService.list(wrapper);
        return R.success(cartList);
    }

    // 添加购物车
    @PostMapping("add")
    public R<String> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        // 少两个数据 number userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        // 根据
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if(dishId != null){ //说明是菜
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{ //就是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one != null){// 查到
            //修改数量
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
            return R.success("添加成功");
        }
        // 执行添加
        shoppingCartService.save(shoppingCart);
        return R.success("添加成功");
    }
}
